package stardust.product.spider;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.ParserException;

import stardust.core.log.Log;

public class FetchHTML
{
	
	public static final Log log = new Log(FetchHTML.class);
	
	private static String titleFilter(Tag tag) throws ParserException
	{
		String result;
		
		if (tag.getTagName().equalsIgnoreCase("title"))
		{
			if (tag.getChildren() != null && tag.getFirstChild().getText() != null)
			{
				return tag.getFirstChild().getText();
			}
		}
		
		if (tag.getChildren() != null)
		{
			NodeIterator ni = tag.getChildren().elements();
			while (ni.hasMoreNodes())
			{
				Node n = ni.nextNode();
				
				if (n instanceof Tag)
				{
					result = titleFilter((Tag) n);
					
					if (result != null)
					{
						return result;
					}
				}
			}
		}
		
		return null;
	}
    
	public static String fetchTitle(String text)
	{
		Parser parser;
		String result;
		
		try
		{
			parser = Parser.createParser(text, "UTF-8");
			
			NodeIterator ni = parser.elements();
			while (ni.hasMoreNodes())
			{
				Node n = ni.nextNode();
				
				if (n instanceof Tag)
				{
					result = titleFilter((Tag) n);
					
					if (result != null)
					{
						return result;
					}
				}
			}
		}
		catch (ParserException e)
		{
			e.printStackTrace();
		}
		
		return "UNKNOW";
	}
	
}
